/**
 * @class   HeyuBinding
 * @brief   
 *
 *
 *
 *
 ****************************************************************************
 * @version $Id: HeyuBinding.h 2762 2010-07-13 15:47:17Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_HeyuBinding_H__
 #define __WOSH_HeyuBinding_H__

 #include <woshDefs.h>
 #include <core/Mutex.h>
 #include <core/BundleGenericWorker.h>
 #include <core/Containers.h>

 #include "DeviceX10.h"
 #include "IHeyuBindingListener.h"


using namespace wosh;

typedef ListT<DeviceX10*> tDeviceX10List;

class HeyuBinding : public BundleGenericWorker  {

	public:

		/// engine's state
		enum HEYU_STATE {
			STATE_UNKNOWN	= 0x00,
			STATE_RUNNING	= 0x01,
			STATE_STOPPED	= 0x02
		 };

		/// engine's events
		enum HEYU_EVENT {
			ENGINE_RECONFIGURATION	= 0x01
		 };

		enum EVENT_SOURCE {
			SOURCE_UNKNOWN		= 0x00,
			SOURCE_COMMANDLINE	= 0x01,
			SOURCE_SCRIPT		= 0x03,
			SOURCE_MACRO		= 0x04,
			SOURCE_MACROEVENT	= 0x0C, ///< 4+8
			SOURCE_POWERLINE	= 0x10,
			SOURCE_CACHE		= 0x20
		 };

	public:
		/**
		 * @brief   Default constructor. Init vars.
		 * @param bundle [in] the parent bundle, see BundleGenericWorker interface
		 */
		HeyuBinding( BundleGeneric& bundle );

		/**
		 * @brief   Deconstructor. If thread is stll running, it will be stopped.
		 */
		virtual ~HeyuBinding();


		WRESULT quitThread( unsigned long synch_timeout = LIMIT_ULONG_MAX );

	public:

		WRESULT turnUnitOn( const std::string& x10code );
		WRESULT turnUnitOff( const std::string& x10code );

		WRESULT setUnitValue( const std::string& x10code, unsigned int value );
		WRESULT setUnitValueOffset( const std::string& x10code, int value );

//		WRESULT turnUnitDim( const std::string& x10code, double dim_value_norm );

//		bool isUnitOn( const std::string& x10code, bool standalone_synch_check = false ) const;
//		bool isUnitOff( const std::string& x10code, bool standalone_synch_check = false  ) const;

//		std::string getX10CodeByAlias( const std::string& alias ) const;
//		std::string getAliasByX10Code( const std::string& x10code ) const;


	public:


		WRESULT updateAll( bool raise_event = false );

		virtual int run_x10_command( const std::string& cmdline );

		WRESULT heyuEngineStart();

		WRESULT heyuEngineStop();

		bool heyuEngineCheckState();

		WRESULT heyuGetInfo();

		WRESULT heyuGetAliases();


/** @name Setters
 * @{
 ******************************************************************************/
 	public:

		void setHeyuBindingListener( IHeyuBindingListener* listener );

		void setHeyuBinary( const std::string& value );
		void setRefreshFrequency( unsigned int ms );
		void setAutoReloadAliases( int seconds  );

//@}
/** @name Getters
 * @{
 ******************************************************************************/
 	public:

		const tDeviceX10List& getDevices() const				{ return this->devices; }
		tDeviceX10List& getDevices()							{ return this->devices; }


		HeyuBinding::HEYU_STATE getState() const				{ return this->heyuState; }

		IHeyuBindingListener* getHeyuBindingListener();

		std::string getHeyuBinary() const;
		std::string getHeyuVersion() const;
		std::string getHeyuConfigFile() const;
		std::string getHeyuPowerLineInt() const;
		std::string getHeyuArmedState() const;

//@}
/** @name Internal methods
 * @{
 ******************************************************************************/
	protected:


		/**
		 * @brief   Perform periodic checkConnected() to update stats.
		 * @see     running
		 * @see     autoRefreshFrequency
		 */
		void runThread();
		bool initializingThread();

		WRESULT clearCache();

		WRESULT runCommandWithRet( const std::string& command, std::vector<std::string>& outputLines );

		WRESULT watchMonitor( bool& stop_request );
		void parseMonitorLine( const char* data, int size, char* iout );

		void allocUnits();


//@}
	protected:

		std::string housecodes;						///<
		std::string heyuBin;						///<

		IHeyuBindingListener* driverX10Listener;	///< callbacks for stats (such as HeyuBundle*)
		MutexRW configMux;							///< Mutex of configuration (previous vars)

		long monitor_process_id;

		HeyuBinding::HEYU_STATE heyuState;			///<

		std::string heyuVersion;					///<
		std::string heyuConfigFile;					///<
		std::string heyuPowerLineInt;				///<
		std::string heyuArmedState;					///<

		int refreshFreq;							///< ms
		int aliasReloadFreq;						///< times of refreshFreq (=multiplier)

		tDeviceX10List devices;

}; // class def


#endif //__WOSH_HeyuBinding_H__
